package com.yykj.hospital.server.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yykj.base.client.feign.BaseFeignClient;
import com.yykj.common.vo.ResultVO;
import com.yykj.hospital.client.dto.DoctorArticleDTO;
import com.yykj.hospital.client.dto.DoctorArticleDetailDTO;
import com.yykj.hospital.server.form.ArticleEditForm;
import com.yykj.hospital.server.form.ArticleSearchForm;
import com.yykj.hospital.server.model.entity.DoctorArticle;
import com.yykj.hospital.server.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api/backstage/hospital/article")
@SuppressWarnings("unchecked")
@Api(tags = "文章管理")
public class ArticleController {

    @ApiOperation("文章--创建")
    @PostMapping("/add")
    @ApiOperationSupport(ignoreParameters = {"id"})
    public ResultVO<DoctorArticle> addArticle(@RequestBody ArticleEditForm form) {
        return ResultVO.success(articleService.editArticle(form));
    }

    @ApiOperation("文章--修改")
    @PutMapping("/edit")
    public ResultVO<DoctorArticle> editArticle(@RequestBody ArticleEditForm form) {
        return ResultVO.success(articleService.editArticle(form));
    }


    @ApiOperation("文章--删除")
    @DeleteMapping("/delete/{id}")
    public ResultVO<DoctorArticle> deleteArticle(@PathVariable Integer id) {
        articleService.deleteArticle(id);
        return ResultVO.success();
    }

    @ApiOperation("文章--查看")
    @GetMapping("/view/{id}")
    public ResultVO<DoctorArticle> viewArticle(@PathVariable Integer id) {
        return ResultVO.success(articleService.viewArticle(id));
    }

    @ApiOperation("医生--文章--查看和是否关注")
    @GetMapping("/doctor/view/article")
    public ResultVO<DoctorArticleDetailDTO> doctorViewArticleDetail(@RequestParam(name = "articleId") Integer articleId,
                                                                    @RequestParam(name = "doctorId") String doctorId) {
        return ResultVO.success(articleService.doctorViewArticleDetail(doctorId, articleId));
    }

    @ApiOperation("文章--列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题"),
            @ApiImplicitParam(name = "hospitalId", value = "医院ID"),
            @ApiImplicitParam(name = "doctorId", value = "医生ID"),
            @ApiImplicitParam(name = "doctorName", value = "医生名称"),
            @ApiImplicitParam(name = "startDate", value = "开始日期,2020-01-01"),
            @ApiImplicitParam(name = "endDate", value = "结束日期"),
            @ApiImplicitParam(name = "searchType", value = "查找类型：1自己的文章，2关注的文章，3好友的文章"),
            @ApiImplicitParam(name = "status", value = "文章启用状态，查找类型为1(自己的文章)时可用"),
            @ApiImplicitParam(name = "currentPage", value = "当前页，默认1"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小:默认10")
    })
    public ResultVO<List<DoctorArticleDTO>> listArticle(@RequestParam(name = "title", required = false) String title,
                                                        @RequestParam(name = "hospitalId", required = false) Integer hospitalId,
                                                        @RequestParam(name = "doctorId", required = false) String doctorId,
                                                        @RequestParam(name = "doctorName", required = false) String doctorName,
                                                        @RequestParam(name = "status", required = false) Boolean status,
                                                        @RequestParam(name = "startDate", required = false) String startDate,
                                                        @RequestParam(name = "endDate", required = false) String endDate,
                                                        @RequestParam(name = "searchType", defaultValue = "1") Integer searchType,
                                                        @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        ArticleSearchForm form = ArticleSearchForm.builder()
                .title(title)
                .hospitalId(hospitalId)
                .doctorId(doctorId)
                .doctorName(doctorName)
                .status(status)
                .startDate(startDate)
                .endDate(endDate)
                .searchType(searchType)
                .currentPage(currentPage)
                .pageSize(pageSize)
                .build();
        return ResultVO.success(articleService.listArticle(form));
    }

    private ArticleService articleService;

    @Autowired
    public void setArticleService(ArticleService articleService) {
        this.articleService = articleService;
    }

}
